﻿using System;
using System.Collections.Generic;
using System.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Navigant.Infrastructure;
using Navigant.Model.Cases;
using Navigant.Model.PreservationNotices;
using Navigant.Infrastructure.DomainBase;
using System.IO;

namespace Navigant.UnitTests.PreservationNoticeTemplates
{
    /// <summary>
    ///This is a test class for CustodianServiceTest and is intended
    ///to contain all CustodianServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class PreservationNoticeTemplateServiceTest
    {
        private TestContext testContextInstance;
        private Uri siteUri;
        private IUnitOfWork unitOfWork;

        public PreservationNoticeTemplateServiceTest()
        {
            this.siteUri = new Uri(ConfigurationManager.AppSettings["testCaseSite"]);
            this.unitOfWork = new UnitOfWork();
        }

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return this.testContextInstance;
            }
            set
            {
                this.testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for GetPreservationNoticeTemplate
        ///</summary>
        [DeploymentItem("TestAttachment.pdf"), TestMethod()]
        public void GetPreservationNoticeTemplateTest()
        {
            this.SaveNewPreservationNoticeTemplate();
            int id = this.GetPreservationNoticeTemplateService().FindAll()[0].Id;
            PreservationNoticeTemplate template = this.GetPreservationNoticeTemplateBy(id);
            Assert.IsTrue(template.Name.Length > 0);
            if (template.Attachments.Count > 0)
            {
                Assert.IsTrue(template.Attachments[0].Content.Length > 1);
            }
        }

        /// <summary>
        ///A test for finding all PreservationNoticeTemplate instances
        ///</summary>
        [DeploymentItem("TestAttachment.pdf"), TestMethod()]
        public void FindAllPreservationNoticeTemplatesTest()
        {
            IList<PreservationNoticeTemplate> templates = this.GetPreservationNoticeTemplateService().FindAll();
            Assert.IsTrue(templates.Count > 0);
            this.testContextInstance.WriteLine("{0} templates found.", templates.Count);
        }

        /// <summary>
        ///A test for saving a new PreservationNoticeTemplate
        ///</summary>
        [DeploymentItem("TestAttachment.pdf"), TestMethod()]
        public void AddPreservationNoticeTemplateTest()
        {
            PreservationNoticeTemplate newPreservationNoticeTemplate = this.SaveNewPreservationNoticeTemplate();
            int newPreservationNoticeTemplateId = newPreservationNoticeTemplate.Id;
            PreservationNoticeTemplate existingPreservationNoticeTemplate = this.GetPreservationNoticeTemplateBy(newPreservationNoticeTemplateId);
            Assert.AreEqual(newPreservationNoticeTemplate.Id, existingPreservationNoticeTemplate.Id);
        }

        /// <summary>
        ///A test for removing a PreservationNoticeTemplate
        ///</summary>
        [DeploymentItem("TestAttachment.pdf"), TestMethod()]
        public void DeletePreservationNoticeTemplateTest()
        {
            PreservationNoticeTemplateService target = this.GetPreservationNoticeTemplateService();
            int oldCount = target.FindAll().Count;
            this.testContextInstance.WriteLine("Old Count:  {0}", oldCount);
            target.RemovePreservationNoticeTemplate(target.FindAll()[0]);
            this.unitOfWork.Commit();
            int newCount = target.FindAll().Count;
            this.testContextInstance.WriteLine("New Count:  {0}", newCount);
            Assert.IsTrue((oldCount - newCount) == 1);
        }

        private PreservationNoticeTemplate GetPreservationNoticeTemplateBy(int id)
        {
            PreservationNoticeTemplateService service = new PreservationNoticeTemplateService(CaseService.GetCaseBy(this.siteUri));
            return service.GetPreservationNoticeTemplate(id);
        }

        private PreservationNoticeTemplateService GetPreservationNoticeTemplateService()
        {
            Case currentCase = CaseService.GetCaseBy(this.siteUri);
            return new PreservationNoticeTemplateService(currentCase, this.unitOfWork);
        }

        private PreservationNoticeTemplate SaveNewPreservationNoticeTemplate()
        {
            PreservationNoticeTemplateService service = this.GetPreservationNoticeTemplateService();
            PreservationNoticeTemplate template = new PreservationNoticeTemplate(CaseService.GetCaseBy(this.siteUri), "Test Template", "This is a test template", "[Image|hi.jpg] </br> The message body for test template.  [URL] ");
            string attachmentFileName = "TestAttachment.pdf";
            byte[] content = null;
            using (Stream stream = File.Open(attachmentFileName, FileMode.Open))
            {
                content = new byte[stream.Length];
                stream.Read(content, 0, (int)stream.Length);
            }
            template.AddAttachment(new Attachment(content, attachmentFileName));
            service.SavePreservationNoticeTemplate(template);
            this.unitOfWork.Commit();
            return template;
        }
    }
}
